package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 商品详情汇总数据
 */
@RestController
@RequestMapping("/api/item")
public class ItemApiController {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

    @GetMapping("/getItem/{skuId}")
    public Map<String,Object> getItem(@PathVariable Long skuId){
        Map<String, Object> result = new HashMap<>();

        //多线程，2、3并行，1、4、5串2后
        //创建线程，返回值给后方串行
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //查询数据(商品微服务)，调用service-product中api的方法（方法已在feign中写成接口）
            //注：存入map的key值需要根据index中需求取
            //2、sku图片及商品信息
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        //串行接收返回值，当前不返回
        CompletableFuture<Void> categoryView = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //1、查询三级分类
            BaseCategoryView baseCategoryView = productFeignClient.getBaseCategoryView(skuInfo.getCategory3Id());
            result.put("categoryView", baseCategoryView);
        }, threadPoolExecutor);

        //创建线程，无返回值，后方无串行
        CompletableFuture<Void> price1 = CompletableFuture.runAsync(() -> {
            //3、sku价格
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        }, threadPoolExecutor);

        //串行接收返回值，当前不返回
        CompletableFuture<Void> spuSaleAttrList = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //4、spu总销售属性，属性值，选中默认商品  -->  前端需要list
            List<SpuSaleAttr> saleAttrAndValeAndChecked = productFeignClient.getSaleAttrAndValeAndChecked(skuId, skuInfo.getSpuId());
            result.put("spuSaleAttrList", saleAttrAndValeAndChecked);
        }, threadPoolExecutor);

        //串行接收返回值，当前不返回
        CompletableFuture<Void> valuesSkuJson = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //5、显示sku多种属性组合  -->  前端item中需要json串，因此map转化
            Map<String, String> skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));//对象转字符串，json.tojsonstring数据转字符串
        }, threadPoolExecutor);

        CompletableFuture.allOf(categoryView,price1,spuSaleAttrList,valuesSkuJson).join();
        return result;
    }


}
